<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-form-item label="学生姓名" prop="xueshengxingming">
        <el-input v-model="dataForm.xueshengxingming" placeholder="学生姓名"></el-input>
      </el-form-item>
      <el-form-item label="请假类型" prop="qingjialeixing">
        <el-select v-model="dataForm.qingjialeixing" placeholder="请选择请假类型">
          <el-option label="事假" value="事假"></el-option>
          <el-option label="病假" value="病假"></el-option>
          <el-option label="其他" value="其他"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="请假时间" prop="qingjiashijian">
        <el-date-picker
          v-model="dataForm.qingjiashijian"
          type="datetime"
          placeholder="选择请假时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="请假天数" prop="qingjiatianshu">
        <el-input-number v-model="dataForm.qingjiatianshu" :min="1" :max="30" label="请假天数"></el-input-number>
      </el-form-item>
      <el-form-item label="请假理由" prop="qingjialiyou">
        <el-input type="textarea" v-model="dataForm.qingjialiyou" placeholder="请假理由" :rows="6"></el-input>
      </el-form-item>
      <el-form-item label="教师审核" prop="laoshishenhe" v-if="dataForm.id">
        <el-select v-model="dataForm.laoshishenhe" placeholder="请选择审核结果">
          <el-option label="待审核" value="待审核"></el-option>
          <el-option label="通过" value="通过"></el-option>
          <el-option label="拒绝" value="拒绝"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="教师批注" prop="laoshipizhu" v-if="dataForm.id">
        <el-input type="textarea" v-model="dataForm.laoshipizhu" placeholder="教师批注" :rows="4"></el-input>
      </el-form-item>
      <el-form-item label="管理员审核" prop="shenhe" v-if="dataForm.id">
        <el-select v-model="dataForm.shenhe" placeholder="请选择审核结果">
          <el-option label="待审核" value="待审核"></el-option>
          <el-option label="通过" value="通过"></el-option>
          <el-option label="拒绝" value="拒绝"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="管理员批注" prop="pizhu" v-if="dataForm.id">
        <el-input type="textarea" v-model="dataForm.pizhu" placeholder="管理员批注" :rows="4"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        xueshengxingming: '',
        qingjialeixing: '',
        qingjiashijian: '',
        qingjiatianshu: 1,
        qingjialiyou: '',
        laoshishenhe: '待审核',
        laoshipizhu: '',
        laoshishenheshijian: '',
        shenhe: '待审核',
        pizhu: '',
        shenheshijian: ''
      },
      dataRule: {
        xueshengxingming: [
          { required: true, message: '学生姓名不能为空', trigger: 'blur' }
        ],
        qingjialeixing: [
          { required: true, message: '请假类型不能为空', trigger: 'change' }
        ],
        qingjiashijian: [
          { required: true, message: '请假时间不能为空', trigger: 'change' }
        ],
        qingjiatianshu: [
          { required: true, message: '请假天数不能为空', trigger: 'blur' }
        ],
        qingjialiyou: [
          { required: true, message: '请假理由不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/qingjiashenqing/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.xueshengxingming = data.qingjiashenqing.xueshengxingming
              this.dataForm.qingjialeixing = data.qingjiashenqing.qingjialeixing
              this.dataForm.qingjiashijian = data.qingjiashenqing.qingjiashijian
              this.dataForm.qingjiatianshu = data.qingjiashenqing.qingjiatianshu
              this.dataForm.qingjialiyou = data.qingjiashenqing.qingjialiyou
              this.dataForm.laoshishenhe = data.qingjiashenqing.laoshishenhe
              this.dataForm.laoshipizhu = data.qingjiashenqing.laoshipizhu
              this.dataForm.laoshishenheshijian = data.qingjiashenqing.laoshishenheshijian
              this.dataForm.shenhe = data.qingjiashenqing.shenhe
              this.dataForm.pizhu = data.qingjiashenqing.pizhu
              this.dataForm.shenheshijian = data.qingjiashenqing.shenheshijian
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/qingjiashenqing/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'xueshengxingming': this.dataForm.xueshengxingming,
              'qingjialeixing': this.dataForm.qingjialeixing,
              'qingjiashijian': this.dataForm.qingjiashijian,
              'qingjiatianshu': this.dataForm.qingjiatianshu,
              'qingjialiyou': this.dataForm.qingjialiyou,
              'laoshishenhe': this.dataForm.laoshishenhe,
              'laoshipizhu': this.dataForm.laoshipizhu,
              'laoshishenheshijian': this.dataForm.laoshishenheshijian,
              'shenhe': this.dataForm.shenhe,
              'pizhu': this.dataForm.pizhu,
              'shenheshijian': this.dataForm.shenheshijian
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-dialog {
  .el-form {
    .el-form-item {
      .el-input {
        width: 100%;
      }
    }
  }
}
</style> 